<template>
  <div class="app-container">
    <!-- <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="轮播描述" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入轮播描述"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="编码" prop="bannerCode">
          <el-input
            v-model="queryParams.bannerCode"
            placeholder="请输入编码"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="归属类型" prop="bannerType">
          <el-select
            v-model="queryParams.bannerType"
            placeholder="归属类型"
            clearable
            style="width: 240px"
          >
            <el-option
              v-for="dict in dict.type.agreement_list_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
  
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
    </el-form>-->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:bannerManage:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:bannerManage:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:bannerManage:del']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['system:dict:export']"
            >导出</el-button>
      </el-col>-->

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="typeList" @selection-change="handleSelectionChange">
      <el-table-column type="expand">
        <template slot-scope="{ row }">
          <el-table :data="row.bannerImageList">
            <el-table-column label="banner图" align="center">
              <template slot-scope="scope">
                <ImagePreview width="160px" height="100px" :src="scope.row.image"></ImagePreview>
              </template>
            </el-table-column>
            <el-table-column label="跳转路径" align="center" prop="jumpUrl" />
            <el-table-column label="排序" align="center" prop="sort" />

            <el-table-column label="类型" align="center">
              <template slot-scope="scope">
                <el-tag>{{ typeArr.find(v=> {return v.value == scope.row.jumpType}).label }}</el-tag>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-table-column>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="轮播描述" align="center" prop="name" />
      <el-table-column label="编码类型" align="center" prop="bannerCode" :show-overflow-tooltip="true" />

      <el-table-column label="归属类型" align="center" prop="bannerType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.banner_attribution_type" :value="scope.row.bannerType" />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="effectStatus" width="180">
        <template slot-scope="scope">
          <el-switch
            active-color="#13ce66"
            active-value="0"
            inactive-value="1"
            v-model="scope.row.effectStatus"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:bannerManage:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:bannerManage:del']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label-width="80px" label="轮播描述" prop="name">
          <el-input v-model="form.name" placeholder="请输入轮播描述" />
        </el-form-item>
        <el-form-item label-width="80px" label="编码" prop="bannerCode">
          <el-input v-model="form.bannerCode" placeholder="请输入编码" />
        </el-form-item>
        <el-form-item label-width="80px" label="归属类型" prop="bannerType">
          <el-select v-model="form.bannerType" placeholder="归属类型" clearable style="width: 240px">
            <el-option
              v-for="dict in dict.type.banner_attribution_type"
              :key="dict.value"
              :label="dict.label"
              :value="Number(dict.value)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label-width="80px" label="状态">
          <el-radio v-model="form.effectStatus" label="0">启用</el-radio>
          <el-radio v-model="form.effectStatus" label="1">禁用</el-radio>
        </el-form-item>
        <div style="text-align: end;">
          <el-button style="height: 36px" type="primary" @click="addPrivateSpec">添加banner图</el-button>
        </div>
        <el-table
          ref="multipleTable"
          :data="form.bannerImageList"
          stripe
          tooltip-effect="dark"
          style="width: 100%; margin-top: 1%"
        >
          <!-- jumpUrl -->
          <el-table-column label="banner图" align="center">
            <template slot-scope="{ row, $index }">
              <el-form-item
                label-width="60px"
                :prop="'bannerImageList.' + $index + '.image'"
                :rules="[
                      {
                        required: true,
                        message: '不能为空',
                        trigger: 'blur',
                      },
                    ]"
              >
                <ImageUploadSmall v-model="row.image" :limit="1" :isShowTip="false"></ImageUploadSmall>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="跳转路径" align="center">
            <template slot-scope="{  row, $index}">
              <el-form-item
                :prop="'bannerImageList.' + $index + '.jumpUrl'"
                :rules="[
                      {
                        required: true,
                        message: '不能为空',
                        trigger: 'blur',
                      },
                    ]"
              >
                <el-input placeholder="请输入跳转路径" v-model="row.jumpUrl"></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="排序" align="center">
            <template slot-scope="{ row,$index}">
              <el-form-item
                :prop="'bannerImageList.' + $index + '.sort'"
                :rules="[
                      {
                        required: true,
                        message: '不能为空',
                        trigger: 'blur',
                      },
                    ]"
              >
                <el-input placeholder="排序" v-model="row.sort"></el-input>
              </el-form-item>
            </template>
          </el-table-column>

          <el-table-column label="类型" align="center">
            <template slot-scope="{ row, $index }">
              <el-form-item
                :rules="[
                      {
                        required: true,
                        message: '不能为空',
                        trigger: 'blur',
                      },
                    ]"
                :prop="'bannerImageList.' + $index + '.jumpType'"
              >
                <el-select placeholder="请选择" v-model="row.jumpType">
                  <el-option
                    v-for="item in typeArr"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="{ row, $index }">
              <el-button size="mini" type="danger" plain @click="delPrivateSpec($index)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
    
 <script>
import ImageUploadSmall from "@/components/ImageUploadSmall/index";
import {
  bannerList,
  delBanner,
  addBanner,
  updateBanner,
  bannereffect
} from "@/api/system/banner";
import Editor from "@/components/Editor";
export default {
  name: "Dict",
  components: {
    Editor,
    ImageUploadSmall
  },
  dicts: ["banner_attribution_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      typeArr: [
        {
          value: 0,
          label: "无跳转"
        },
        {
          value: 1,
          label: "内部页面"
        },
        {
          value: 2,
          label: "H5"
        },
        {
          value: 3,
          label: "商品"
        }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        bannerCode: undefined,
        bannerType: undefined,
        effectStatus: undefined
      },
      // 表单参数
      form: {
        bannerImageList: []
      },
      bannerId: "",
      // 表单校验
      rules: {
        name: [
          { required: true, message: "轮播描述不能为空", trigger: "blur" }
        ],
        bannerCode: [
          { required: true, message: "编码不能为空", trigger: "blur" }
        ],
        bannerType: [
          { required: true, message: "归属类型不能为空", trigger: "blur" }
        ],
        docContent: [
          { required: true, message: "协议内容不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    addPrivateSpec() {
      console.log(this.form.bannerImageList);
      this.form.bannerImageList.push({
        jumpUrl: "",
        sort: this.form.bannerImageList.length,
        jumpType: "",
        image: ""
      });
    },
    delPrivateSpec(index) {
      this.form.bannerImageList.splice(index, 1);
    },
    /** 查询字典类型列表 */
    getList() {
      this.loading = true;
      bannerList(this.addDateRange(this.queryParams, this.dateRange)).then(
        response => {
          this.typeList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bannerId: undefined,
        name: undefined,
        bannerCode: undefined,
        bannerType: "1",
        docContent: undefined,
        effectStatus: "0",
        bannerImageList: []
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.bannerId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.bannerId = row.bannerId;
      this.form = row;
      this.open = true;
      this.title = "修改";
    },
    handleStatusChange(row) {
      let text = row.effectStatus === "0" ? "启用" : "禁用";
      this.$modal
        .confirm('确认要"' + text + "该条数据吗？")
        .then(function() {
          return bannereffect(row.bannerId, row.effectStatus);
        })
        .then(() => {
          this.$modal.msgSuccess(text + "成功");
        })
        .catch(function() {
          row.effectStatus = row.effectStatus === "0" ? "1" : "0";
        });
    },
    /** 提交按钮 */
    submitForm: function() {
      if (this.form.bannerImageList.length < 1)
        return this.$message.error("请添加banner图");
      this.$refs["form"].validate(valid => {
        if (valid) {
          let params = JSON.parse(JSON.stringify(this.form));
          if (this.bannerId) {
            updateBanner(params).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBanner(params).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const bannerIds = row.bannerId || this.ids;
      this.$modal
        .confirm("是否确认删除该条数据？")
        .then(function() {
          return delBanner(bannerIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/dict/type/export",
        {
          ...this.queryParams
        },
        `type_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>
    